package com.tj.jinzao.fragment;

import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tj.jinzao.App;
import com.tj.jinzao.R;
import com.tj.jinzao.adapter.CityItemAdapter;
import com.tj.jinzao.base.BaseCursorListFragment;
import com.tj.jinzao.base.RecyclerViewCursorAdapter;
import com.tj.jinzao.model.City;
import com.tj.jinzao.presenter.CityPresenter;
import com.tj.jinzao.provider.ProviderContract;
import com.tj.jinzao.utils.Utils;

import java.util.List;

import butterknife.BindView;


/**
 * 等级列表Fragment
 * Created by wjy on 2018/5/22.
 */
public class CityFragment extends BaseCursorListFragment<List<City>, CityPresenter> {

    private String mCityName;
    @BindView(R.id.ll_label_layout)
    LinearLayout mLabelLayout;
    private List<String> mLabelList;
    private View mCurrentView;

    @BindView(R.id.tv_city)
    TextView mCityView;

    @Override
    public void onRequestSuccess(List<City> list) {
    }

    @Override
    protected boolean resolveData(List<City> cities) {
        return false;
    }


    private void setCityList() {
        mLabelList = mPresenter.getCityList();
        mLabelLayout.removeAllViews();
        int padding = Utils.dpToPx(10, getResources());
        for (String name : mLabelList) {
            TextView textView = new TextView(mContext);
            textView.setTextColor(Color.parseColor("#535671"));
            textView.setTextSize(14);
            textView.setOnClickListener(this);
            textView.setTag(R.id.position, name);
            textView.setText(name);
            textView.setPadding(0, padding, 0, padding);
            textView.setBackgroundResource(R.color.activity_bg);
            textView.setGravity(Gravity.CENTER);
            if (mCurrentView == null) {
                mCityName = name;
                mCurrentView = textView;
                mCurrentView.setBackgroundResource(R.drawable.bg_occupation_label);
            }
            mLabelLayout.addView(textView);
        }
        restartLoader(mLoaderId);
    }


    @Override
    protected CityPresenter createPresenter() {
        return new CityPresenter(this);
    }

    @Override
    public void onRequestNoData(String message) {
        setEmptyDataView();
    }

    @Override
    protected RecyclerViewCursorAdapter createCursorAdapter() {
        return new CityItemAdapter(mContext, null);
    }

    @Override
    protected Loader<Cursor> createLoader(int id, Bundle bundle) {
        return new CursorLoader(getActivity()
                , ProviderContract.City.CONTENT_URI, null
                , ProviderContract.City.FATHER_NAME + "=? "
                , new String[]{mCityName}, null);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        String name = (String) v.getTag(R.id.position);
        if (TextUtils.equals("全国城市", name)) {
            City city = new City();
            city.setName(name);
            App.sCity = city;
            mContext.finish();
            return;
        }
        if (!TextUtils.equals(mCityName, name)) {
            mCityName = name;
            mCurrentView.setBackgroundResource(R.color.activity_bg);
            v.setBackgroundResource(R.drawable.bg_occupation_label);
            mCurrentView = v;
            restartLoader(mLoaderId);
        }
    }

    @Override
    protected void itemClicked(Cursor cursor) {
        String cityName = cursor.getString(cursor.getColumnIndex(ProviderContract.City.CITY_NAME));
        City city = new City();
        city.setName(cityName);
        App.sCity = city;
        mContext.finish();
    }

    @Override
    protected void createView(LayoutInflater inflater, View view) {
        setCityList();
        if (App.sCity != null) {
            mCityView.setText(App.sCity.getFather_name());
        }
    }

    @Override
    protected boolean canLoadMoreItems() {
        return false;
    }

    @Override
    protected void requestData() {
    }

    @Override
    protected void setEmptyDataView() {
        setEmptyView("没有数据");
    }

}
